package cn.webrx.bs.servlet;

import cn.webrx.bs.entity.Address;
import cn.webrx.bs.service.AddressDao;
import cn.webrx.bs.service.AddressDaoImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * <p>功能描述: </p>
 *
 * @author webrx
 * @version 1.0
 * @date 2020-08-19 11:51
 */
@WebServlet("/u")
public class Test extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String u = "u";
        if ("cate".equals(req.getParameter("action"))) {
            //三级
            AddressDao adao = new AddressDaoImpl();
            u = "menu";
            List<Address> ss1 = adao.queryByParendId(0);
            req.setAttribute("s1", ss1);

            //
            List<Address> ss2 = adao.queryByParendId(ss1.get(0).getId());
            System.out.println(ss2);
            req.setAttribute("s2", ss2);


            req.setAttribute("s1id", "");

            if (req.getParameter("catesub") != null) {
                //req.setAttribute("s11",req.getParameter("catesub"));
                req.setAttribute("s1id", req.getParameter("catesub"));

                List<Address> list = adao.queryByParendId(Integer.parseInt(req.getParameter("catesub")));
                req.setAttribute("s2", list);

                List<Address> list2 = adao.queryByParendId(list.get(0).getId());
                if (list2 == null || list2.isEmpty()) {
                } else {
                    req.setAttribute("s3", list2);
                }


            }
            if (req.getParameter("catesub2") != null) {
                //req.setAttribute("s",req.getParameter("catesub"));
                List<Address> list3 = adao.queryByParendId(Integer.parseInt(req.getParameter("catesub2")));
                if (list3 == null) {
                } else {
                    req.setAttribute("s3", list3);
                }
            }
        }
        if ("sub".equals(req.getParameter("action"))) {
            req.setAttribute("author", req.getParameter("author"));
            req.setAttribute("content", req.getParameter("content"));
            u = "show";
        }
        req.getRequestDispatcher("/" + u + ".ftl").forward(req, resp);
    }
}
